<?php
require_once '../include/conn.php';
require_once 'header.php';
$webkefu = $db->get_one("SELECT * FROM `h_kefu_config`");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>聊天窗口</title>
    <link rel="stylesheet" href="/style.css">
</head>
<body>
    <div class="pc-main clearfix">
        <div class="pc-left">
            <div class="pc-service">
                <div class="service-box">
                    <div class="user-box">
                        <?php
                            $rs4 = $db->get_one("select count(*) as tj from `h_kefu`where h_who = 1 and h_isread = 0");                   
                            if($rs4['tj'] > 0 && $webkefu['h_sound'] == 1){
                                echo '<audio src="admin.mp3" autoplay></audio>';
                            }
                            $query = "select h_user from `h_kefu` group by h_user order by max(h_addTime) desc,max(id) desc";
                            $result = $db->query($query);
                            while($rs_list = $db->fetch_array($result))
                            {	
                                $rs1 = $db->get_one("select * from `h_kefu` where h_user = '{$rs_list['h_user']}' order by h_addTime desc,id desc limit 1");
                                $rs1['h_addTime'] = date("m-d H:i",strtotime($rs1['h_addTime']));
                                $rs2 = $db->get_one("select count(*) as tj from `h_kefu`where h_user = '{$rs_list['h_user']}' and h_who = 1 and h_isread = 0");
                                $rs3 = $db->get_one("select * from `h_kefu_status` where h_user = '{$rs_list['h_user']}'");
                                $date_1 = date("Y-m-d H:i:s"); 
                                $date_2 = $rs3['h_addTime'];
                                $time=ceil(strtotime($date_1)-strtotime($date_2));
                                if($rs3['h_stop'] == 1){
                                    $status = '黑名单';
                                }else{
                                    if($time > 3){
                                        $status = '离线';
                                    }else{
                                        if($rs3['h_status'] == 1){$status = '正在会话';}else if($rs3['h_status'] == 2){$status = '隐藏窗口';}
                                    }
                                }
                                if($rs3['h_name'] == ''){
                                    $name = $rs_list['h_user'];
                                }else{
                                    $name = $rs3['h_name'];
                                }
                                echo '
                                    <a href="talking.php?user=' . $rs_list['h_user'] . '" target="pc-page">
                                        <div class="user-list">
                                            <img src="/img/user_1.png" alt="">
                                            <div class="user">
                                                <div class="name">' . $name . '<span class="ison">' . $status . '</span></div>
                                                <div class="last-msg">' . $rs1['h_content'] . '</div>
                                            </div>
                                            <div class="data">
                                                <div class="time">' . $rs1['h_addTime'] . '</div>
                                                <div class="num">' .  $rs2['tj'] . '</div>
                                            </div>
                                        </div>
                                    </a>
                                ';
                            }
                        ?>
                    </div>
                </div>
                <div class="foot">
                    <div class="sound">
                        <?php if($webkefu['h_sound'] == 1){
                                echo '<a href="?act=soundoff">关闭声音</a>';
                            }else{
                                echo '<a href="?act=soundon">开启声音</a>';
                            }
                        ?>
                    </div>
                    <div class="robot">
                        <?php if($webkefu['h_robot'] == 1){
                                echo '<a href="?clause=robotoff">关闭机器人</a>';
                            }else{
                                echo '<a href="?clause=roboton">开启机器人</a>';
                            }
                        ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="pc-right">
            <iframe src="talking.php" name="pc-page" frameborder="0" width="100%" height="100%"></iframe>
        </div>
    </div>
    <script src="/js/jquery.min.js"></script>
    <script>
        setInterval(function () {
            $(".service-box").load(location.href + " .service-box");
        }, 1500);
    </script>
    <?php 
    if($act == 'soundoff'){soundoff();}
    if($act == 'soundon'){soundon();}
    if($clause == 'robotoff'){robotoff();}
    if($clause == 'roboton'){roboton();}

    function soundoff()
    {
        global $db;
        $query = "update `h_kefu_config` set h_sound = 0";
        $db->query($query);

        echo("<script type='text/javascript'> location.href='?'; </script>");
        exit;
        
    } 
    function soundon()
    {
        global $db;
        $query = "update `h_kefu_config` set h_sound = 1";
        $db->query($query);

        echo("<script type='text/javascript'> location.href='?'; </script>");
        exit;  
    }   
    function robotoff()
    {
        global $db;
        $query = "update `h_kefu_config` set h_robot = 0";
        $db->query($query);

        echo("<script type='text/javascript'> location.href='?'; </script>");
        exit;  
    } 
    function roboton()
    {
        global $db;
        $query = "update `h_kefu_config` set h_robot = 1";
        $db->query($query);
        
        echo("<script type='text/javascript'> location.href='?'; </script>");
        exit;  
    }      
    ?>
</body>
</html>